package flc.ast.fragment;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import flc.ast.R;
import flc.ast.databinding.FragmentHouseBinding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HouseFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HouseFragment extends BaseNoModelFragment<FragmentHouseBinding> {

    private Fragment mCurShowFragment;
    private Fragment houseKind1Fragment,houseKind2Fragment,houseKind3Fragment;

    @Override
    protected int onCreate() {
        return R.layout.fragment_house;
    }

    @Override
    protected void initView() {
        mDataBinding.tvHouseKind1.setOnClickListener(this);
        mDataBinding.tvHouseKind2.setOnClickListener(this);
        mDataBinding.tvHouseKind3.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvHouseKind1:
                clearSelection();
                showHouseKind1Fragment();
                mDataBinding.tvHouseKind1.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvHouseKind1.setTextColor(Color.WHITE);
                mDataBinding.tvHouseKind1.setBackgroundResource(R.drawable.ak5);
                mDataBinding.llKindTiTle.setBackgroundResource(R.drawable.ak4);
                break;
            case R.id.tvHouseKind2:
                clearSelection();
                showHouseKind2Fragment();
                mDataBinding.tvHouseKind2.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvHouseKind2.setTextColor(Color.WHITE);
                mDataBinding.tvHouseKind2.setBackgroundResource(R.drawable.ak5);
                mDataBinding.llKindTiTle.setBackgroundResource(R.drawable.ak7);
                break;
            case R.id.tvHouseKind3:
                clearSelection();
                showHouseKind3Fragment();
                mDataBinding.tvHouseKind3.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvHouseKind3.setTextColor(Color.WHITE);
                mDataBinding.tvHouseKind3.setBackgroundResource(R.drawable.ak5);
                mDataBinding.llKindTiTle.setBackgroundResource(R.drawable.ak8);
                break;
        }
    }

    @Override
    protected void initData() {
        showHouseKind1Fragment();
    }

    //显示商业贷款模块
    private void showHouseKind1Fragment() {
        if (houseKind1Fragment == null) {
            houseKind1Fragment = new HouseKind1Fragment();
        }
        showFragment(houseKind1Fragment);
    }

    //显示公积金模块
    private void showHouseKind2Fragment() {
        if (houseKind2Fragment == null) {
            houseKind2Fragment = new HouseKind2Fragment();
        }
        showFragment(houseKind2Fragment);
    }

    //显示组合贷款模块
    private void showHouseKind3Fragment() {
        if (houseKind3Fragment == null) {
            houseKind3Fragment = new HouseKind3Fragment();
        }
        showFragment(houseKind3Fragment);
    }

    private void showFragment(Fragment fragment) {
        if (fragment == null) return;
        if (fragment.isAdded() && fragment.isVisible()) return;
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        if (mCurShowFragment != null) {
            transaction.hide(mCurShowFragment);
        }
        if (!fragment.isAdded()) {
            transaction.add(R.id.fl_container, fragment);
        } else {
            transaction.show(fragment);
        }
        mCurShowFragment = fragment;
        transaction.commit();
    }

    private void clearSelection() {
        mDataBinding.tvHouseKind1.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvHouseKind1.setBackground(null);
        mDataBinding.tvHouseKind1.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvHouseKind2.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvHouseKind2.setBackground(null);
        mDataBinding.tvHouseKind2.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvHouseKind3.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvHouseKind3.setBackground(null);
        mDataBinding.tvHouseKind3.setTypeface(Typeface.DEFAULT);
    }

}